<?php
/**
 * 页面输出类，封装了常用的页面输出操作
 * 
 * <code>
 * 		$pagebuilder = new PageBuilder();
 * 		$pagebuilder->loadCss('jquery.autocomplete.css');
 * 		$pagebuilder->show('twitter.php');
 * </code>
 * 
 * 文件头定义了关于路径处理的常量前缀。所有的模板文件中的有关路径的操作均应加入常量前缀以使整个项目的路径结构清晰可维护
 */
//引入配置文件
require_once(dirname(__FILE__).'/../config/UCConfMacro.php');
require_once(dirname(__FILE__).'/../config/UCConfPage.php');
require_once(dirname(__FILE__).'/../config/UCConfStore.php');
//引入User_Info模块
require_once(dirname(__FILE__).'/../models/user/UCModUserInfo.php');
//引入App模块
require_once(dirname(__FILE__).'/../models/store/UCModStore.php');
require_once(dirname(__FILE__).'/../models/store/UCModPanel.php');
require_once(dirname(__FILE__).'/../models/store/UCModStore.php');
//引入勋章类
require_once(dirname(__FILE__).'/../models/medal/UCModMedalInterface.php');
//引入通用函数
require_once('UCFuncUtils.php');

class UCLibPageBuilder {
	
	//Appbar样式常量
	const __APP_BAR_WIDE = 1;
	const __APP_BAR_NARROW = 2;
	//压缩js文件
	static $compressjs = array();
	
	//包含js
	public $includejs = array();
	//包含css
	public $includecss = array();
	//包含title
	public $title;
	//包含keywords
	public $keywords;
	//包含description
	public $description;
	//左侧appbar样式
	private $_appbartype = self::__APP_BAR_NARROW;
	//是否输出公共头尾
	private $_showall = true;
	//是否输出app列表
	private $_showmenu = true;
	
	//页面控制器
	public $controller;
	//页面动作
	public $action;
	//pb数据
	public $pbdata = array();
	//app数据
	public $current_app;
	//建立页面的user_id,此user_id表示呈现的页面的主人
	public $user_id = null;
	//通过user_id取得的user_info对象
	public $user_info = null;
    //当前登录用户的PT帐号
    public $ptid = null;
	//css版本控制
	public $css_version = null;
	
	//生成url函数
	static public function createUrl($route, $params = array(), $ampersand='&') {
		$param_string = '';
		//无参数
		if(empty($params))
			return __SITE_URL.'/'.$route;
		//循环遍历参数
		foreach($params as $key => $param) {
			$param_string .= $key.'='.$param.$ampersand;
		}
		$param_string = substr($param_string, 0, strlen($param_string)-1);
		return __SITE_URL.'/'.$route.'/?'.$param_string;
	}
	
	/**
	 * 构造函数
	 */
	public function __construct($user_id, $ptid)
	{
		if(defined('__UC_ENV') && __UC_ENV == 'DEV') {
			self::$compressjs = array();
			$this->loadScript('core/jquery-1.4.2.js','lib/constant.js', 'core/common.js', 'lib/data.js', 'uclib/component/dialog.js', 'uclib/business/account.js', 'plugins/jquery.cookie.js', 'uclib/component/stat.js', 'uclib/component/store.js');
		} else {
			self::$compressjs = uc_confpage_compressjs();
			$this->loadScript('lib/constant.js', 'uc_all.js');
		};
		
		//用户信息初始化
		$this->user_id = $user_id;
		$this->ptid = $ptid;
		if($this->user_id) $this->user_info = UCModUserInfo::getUser($this->user_id);
		//全局加载js,css
        $this->loadCss('base.css', 'popup.css');	
	}
	
	/**
	 * 设置appbar样式
	 * @param integer $type 取值为：self::__APP_BAR_WIDE或self::__APP_BAR_NARROW
	 */
	public function setAppbarType($type)
	{
		$this->_appbartype = $type;
	}
	
	/**
	 * 取消载入css文件
	 * @param args $cssfile1, $cssfile2,... css文件名
	 */
	public function unloadCss()
	{
        $arg_list = func_get_args();
        foreach ($arg_list as $arg) {
            if(in_array($arg, $this->includecss)){
            	$k = array_search($arg, $this->includecss);
            	unset($this->includecss[$k]);
            }
        }
	}
	
	/**
	 * 设置user_id
	 * @param integer $user_id
     * @param string $ptid
	 */
	public function setUserId($user_id, $ptid=null)
	{
		$this->user_id = $user_id;
        $this->ptid = $ptid;
	}
	
	/**
	 * 设置页面的控制器和动作
	 * @param string $controller
	 * @param string $action
	 */
	public function setControllerandAction($controller, $action)
	{
		$this->controller = $controller;
		$this->action = $action;
	}
	
	/**
	 * 设置当前激活的app
	 * @param string $app
	 */
	public function setCurrentApp($app)
	{
		$this->current_app = $app;
	}
	
	/**
	 * 设置PageBulider模板数据
	 * @param array $pbdata 模板数据
	 */
	public function setPbData($pbdata)
	{
		if(is_array($pbdata))
			$this->pbdata = $pbdata;
	}
	
	/**
	 * 设置是否显示全部，包括头尾以及左侧边栏
	 * @param boolean $bool true显示头尾，false不显示头尾
	 */
	public function setShowAll($bool)
	{
		$this->_showall = $bool;
	}
	
	public function setShowMenu($bool)
	{
		$this->_showmenu = $bool;
	}
	
	/**
	 * 显示头部信息
	 */
	private function _showHeader(){
		//给每个css文件附上路径前缀
		foreach($this->includecss as $value){
			$value = __STATIC_URL.'/css/'.$value.'?'.__JS_VERSION;
            $includecss[] = $value;
		}
		//给每个js文件附加路径前缀
		foreach($this->includejs as $value){
			$value = __STATIC_URL.'/js/'.$value.'?'.__CSS_VERSION;
			$includejs[] = $value;
		}
		
		//设置title
		$includetitle = $this->title;
		//设置keywords
		$includekeywords = $this->keywords;
		//设置description
		$includedescription = $this->description;
		//用户名
		$user_name = $this->user_info->getName();
		$offical = $this->user_info->getValue(USERINFO_AUTHED_FLAG);
		
		//全站错误！
		//if($user_name===false) die('内部错误！');

		//拼装数据
		$showdata = array('user_name' => $user_name, 'offical' => $offical);
		$this->pbdata = array_merge($this->pbdata, $showdata);
		//输出头部html
		require_once(__TEMPLATE_ROOT.'/header.php');
	}
	
	/**
	 * 显示样式Appbar
	 */
	private function _showAppbar(){
		
		//需要显示的app列表
		$o_panel = new UCModPanel($this->user_id);
		$data = $o_panel->get();
		$page = 1;
		$size = UCConfStore::$panel_psize;
		if(false == $data)
			$data = array();

		$total = count($data);
		if(($page - 1) * $size > $total)
		{
			$app_list = array();
		}else{
			$app_list = array_slice($data, ($page - 1) * $size, $page * $size);
		}
		foreach($app_list as $idx => $app)
		{
			$app_list[$idx]['icon32'] = UCModPanel::getIcon($app['id'], '32');
			$app_list[$idx]['icon50'] = UCModPanel::getIcon($app['id'], '50');
		}
		$appArr=array('entry' => $app_list, 'size' => $size, 'page' => $page, 'total' => $total);
		$encodeApp=json_encode($appArr);
		if ($this->_appbartype == self::__APP_BAR_WIDE){
			$avatar_l = $this->user_info->getAvatar(USERINFO_AVATAR_90);
			$avatar_version = $this->user_info->getValue(USERINFO_AVATAR_VERSION);
			//勋章
			$medal_list = UCModMedalInterface::getUserAllMedal($this->user_id, 6);
			//拼装显示数据
			$this->pbdata = array_merge($this->pbdata, array('medal_list' => $medal_list['list'], 'avatar_l' => $avatar_l, 'avatar_version' => $avatar_version));
			include_once(__TEMPLATE_ROOT.'/appmenu_wide.php');
		}else{
			include_once(__TEMPLATE_ROOT.'/appmenu.php');
		}
	}
	
	/**
	 * 显示页脚
	 */
	private function _showFooter(){
		require_once(__TEMPLATE_ROOT.'/footer.php');
	}
	
	/**
	 * 显示页面主体部分
	 */
	public function show($containerhtml = null, $return = false){

		//输出保持
		ob_start();
		ob_implicit_flush(false);
		
		if($this->_showall){
		
			$this->_showHeader();
			
			if($this->_showmenu){
				$this->_showAppbar();
			}
		}
		
        if( isset($containerhtml) ) //输出核心内容   
		    echo $containerhtml;
		
		if($this->_showall) {
			UCLibStat::pv(UCConfStat::STAT_PV,'/');
			UCLibStat::statex(UCConfStat::STAT_IP,get_client_ip());
			UCLibStat::statex(UCConfStat::STAT_LIST_DIST,$this->user_id.'|'.get_client_ip());
			$this->_showFooter();
		}
        
		//返回而不输出
		if($return){
			return ob_get_clean();
		}
		ob_get_contents();
	}
	
	/**
	 * 输出body部分，不含头部导航及左侧标签
	 */
	public function showonlybody($bodyhtml = null, $return = false) {

		//给每个css文件附上路径前缀
		foreach($this->includecss as $value){
			$value = __STATIC_URL.'/css/'.$value.'?'.__JS_VERSION;
            $includecss[] = $value;
		}

		//给每个js文件附加路径前缀
		foreach($this->includejs as $value){
			$value = __STATIC_URL.'/js/'.$value.'?'.__CSS_VERSION;
			$includejs[] = $value;
		}

		//设置title
		$includetitle = $this->title;
		//设置keywords
		$includekeywords = $this->keywords;
		//设置description
		$includedescription = $this->description;
		
		//echo $includetitle;
		//echo $this->description;
		
		//输出保持
		ob_start();
		ob_implicit_flush(false);

		require_once(__TEMPLATE_ROOT.'/header_pre.php');

        if( isset($bodyhtml) ) //输出核心内容   
		    echo $bodyhtml;

		echo '</html>';

		//返回而不输出
		if($return){
			return ob_get_clean();
		}
		ob_get_contents();
	}

	/**
	 * 载入css文件
	 * @param args $cssfile1, $cssfile2,... css文件名
	 */
	public function loadCss()
	{
        $arg_list = func_get_args();
        foreach ($arg_list as $arg) {
            if(!in_array($arg, $this->includecss)){
                array_push($this->includecss, $arg);
            }
        }
	}
	
	/**
	 * 载入脚本文件
	 * @param args $scriptfile1, $scriptfile2,... js文件名
	 */
	public function loadScript()
	{
		$arg_list = func_get_args();
        foreach ($arg_list as $arg) {
        	if(in_array($arg, self::$compressjs)) {
        		continue;
        	}
            if(!in_array($arg, $this->includejs)){
                array_push($this->includejs, $arg);
            }
        }
	}
	
	/**
	 * 设置页面title
	 * @param string
	 */
	public function setTitle($title)
	{
		$this->title = $title;
	}
	
	/**
	 * 设置页面keywords
	 * @param string $keywords
	 */
	public function setKeywords($keywords)
	{
		$this->keywords = $keywords;
	}
	
	/**
	 * 设置页面的description
	 * @param string $description
	 */
	public function setDescription($description)
	{
		$this->description = $description;
	}
}
?>